package com.nsgf.library.widget.prckerview;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.webkit.JavascriptInterface;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.google.gson.Gson;
import com.kunminx.architecture.ui.page.DataBindingConfig;
import com.nsgf.library.BR;
import com.nsgf.library.R;
import com.nsgf.library.data.api.APIs;
import com.nsgf.library.data.bean.LibraryInfo;
import com.nsgf.library.databinding.FargmentNsgfWebviewBinding;
import com.nsgf.library.ui.GotoActivity;
import com.nsgf.library.ui.monitoring.DisplacementActivity;
import com.nsgf.library.ui.monitoring.GNSSActivity;
import com.nsgf.library.ui.monitoring.OsmoticPressureActivity;
import com.nsgf.library.ui.page.BaseFragment;
import com.nsgf.library.ui.page.HomeMoreActivity;
import com.nsgf.library.ui.page.StateHolder;
import com.nsgf.library.ui.monitoring.TransfusionActivity;
import com.nsgf.library.ui.page.WebViewActivity;
import com.nsgf.library.ui.page.video.VideoListActivity;
import com.nsgf.library.ui.page.video.VideoRegionListActivity;
import com.nsgf.library.ui.page.warehouse.WareHouseListActivity;
import com.nsgf.library.ui.workbenches.PatrolFeedActivity;

public class WebViewFragment extends BaseFragment {

    private StateHolder mState;

    public static WebViewFragment newInstance(boolean type) {
        WebViewFragment webViewFragment = new WebViewFragment();
        Bundle bundle = new Bundle();
        bundle.putBoolean("type", type);
        webViewFragment.setArguments(bundle);
        return webViewFragment;

    }

    @Override
    protected void initViewModel() {
        mState = getActivityScopeViewModel(StateHolder.class);
    }

    private FargmentNsgfWebviewBinding mBinding;

    @Override
    protected DataBindingConfig getDataBindingConfig() {
        return new DataBindingConfig(R.layout.fargment_nsgf_webview, BR.vm, mState);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        mBinding = (FargmentNsgfWebviewBinding) getBinding();
        mBinding.webView.getWebView().addJavascriptInterface(new TestInterface(), "android");
        mBinding.webView.loadUrl(getArguments().getBoolean("type") ? mState.H5_MONITORING_REGIN.get() : mState.H5.get());

    }

    /**
     * Js调用的JavascriptInterface
     */
    public class TestInterface {

        @JavascriptInterface
        public void callNativeMethod(String src) {
//            {"path":"/monitoring/osmoticPressure","title":"渗压监测"}
            Gson gson = new Gson();
            LibraryInfo info = gson.fromJson(src, LibraryInfo.class);
            // 当用户点击链接时，不在WebView中打开，而是启动新的Activity
            Intent intent = new Intent(mActivity, WebViewActivity.class);
            intent.putExtra("url", APIs.HTTP_H5 + info.getPath());
            intent.putExtra("title", info.getTitle());
            startActivity(intent);
        }

        @JavascriptInterface
        public void callDisplacement() {
            Intent intent = new Intent(getActivity(), DisplacementActivity.class);
            startActivity(intent);
        }

        @JavascriptInterface
        public void callGSNN() {
            Intent intent = new Intent(getActivity(), GNSSActivity.class);
            startActivity(intent);
        }

        @JavascriptInterface
        public void callTransfusion() {
            Intent intent = new Intent(getActivity(), TransfusionActivity.class);
            startActivity(intent);
        }

        @JavascriptInterface
        public void callOsmoticPressure() {
            Intent intent = new Intent(getActivity(), OsmoticPressureActivity.class);
            startActivity(intent);
        }

        @JavascriptInterface
        public void callVideo() {
            Intent intent = new Intent(getActivity(), VideoListActivity.class);
            startActivity(intent);
        }

        @JavascriptInterface
        public void callRegionVideo() {
            Intent intent = new Intent(getActivity(), VideoRegionListActivity.class);
            startActivity(intent);
        }

        @JavascriptInterface
        public void callHouseInfo() {
            Intent intent = new Intent(getActivity(), WareHouseListActivity.class);
            startActivity(intent);
        }

        @JavascriptInterface
        public void callWarning() {
            GotoActivity.gotoWarningActivity(getActivity(), "1");
        }

        @JavascriptInterface
        public void callPatrolFeed(String title) {
            Intent intent = new Intent(getActivity(), PatrolFeedActivity.class);
            intent.putExtra("title", title);
            startActivity(intent);
        }

        @JavascriptInterface
        public void callWater(String title, int type) {
            Intent intent = new Intent(getActivity(), HomeMoreActivity.class);
            intent.putExtra("title", title);
            intent.putExtra("type", type);
            startActivity(intent);
        }


        @JavascriptInterface
        public void callVideo(String src) {
            Intent intent = new Intent(getActivity(), VideoListActivity.class);
            startActivity(intent);
        }

    }

}
